<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_TranslatePageAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$docId = $this->getRequestParameter("docId");
		$docLang = $this->getRequestParameter("docLang");
		$values = $this->getFormValues("form");
		
		$destinationDocument = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $docId, $docLang);
		$originalDocument = $destinationDocument->getVoDocument();

		if(count($values) > 0)
		{
			switch ($values["transType"]) {
				case "0":
					$this->addToModel("error", array("display" => "false"));
				break;
				
				case "1":
                    $transTemplate = isset($values['transTemplates']) ? $values['transTemplates'] : null;
					return $this->translateToEmptyPage($originalDocument, $destinationDocument, $transTemplate);
				break;	
				
				case "2":
					return $this->translateToEmptyPageWithTemplate($originalDocument, $destinationDocument);
				break;
				
				case "3":
					return $this->translateWithEmptyStructure($originalDocument, $destinationDocument);
				break;
				
				case "4":
					return $this->translateWithContent($originalDocument, $destinationDocument);
				break;
				
				default:
					$this->addToModel("error", array("display" => "false"));
				break;
			}
			
		}
		else
		{
			$destinationDocument->setUrl($originalDocument->getFormattedUrl());
		}

        $templates = m_website_services_PageService::getInstance()->getTemplates();
        
		$this->addToModel("destinationDocument", $destinationDocument);
		$this->addToModel("originalDocument", $originalDocument);
        $this->addToModel("templates", $templates);
		$this->addToModel("destLang", strtoupper($docLang));

		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_Translation"));
	}
	
	private function translateToEmptyPage($originalDocument, $destinationDocument, $transTemplate)
	{
			$websiteId = $originalDocument->getWebsite()->getUniqueId();
			$parent = $this->getParent($originalDocument, $destinationDocument);
                            
			$doc = $destinationDocument	->setWebsiteId($websiteId)
										->setAuthor($this->getUser()->getLogin())
										->setUrl($originalDocument->getUrl())
                                        ->setTemplate($transTemplate)
										->setLabel($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
                                        ->setMetaTitle($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
										->setPublicationstatus("NEW")
                                        ->setIndexable(true)
										->save(false);
										
			$parent->addPage($doc)->save(false);
			return "redirectUrl=".$doc->getEditUrl($doc->getLang());
	}
	
	private function translateToEmptyPageWithTemplate($originalDocument, $destinationDocument)
	{
			$websiteId = $originalDocument->getWebsite()->getUniqueId();					
			$parent = $this->getParent($originalDocument, $destinationDocument);
							
			$doc = $destinationDocument	->setWebsiteId($websiteId)
										->setAuthor($this->getUser()->getLogin())
										->setTemplate($originalDocument->getTemplate())
										->setUrl($originalDocument->getUrl())
										->setLabel($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
                                        ->setMetaTitle($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
										->setPublicationstatus("DRAFT")
                                        ->setIndexable(true)
										->save(false);
										
			$parent->addPage($doc)->save(false);
			return "redirectUrl=".$doc->getEditUrl($doc->getLang());
	}
	
	private function translateWithEmptyStructure($originalDocument, $destinationDocument)
	{
			$websiteId = $originalDocument->getWebsite()->getUniqueId();					
			$parent = $this->getParent($originalDocument, $destinationDocument);
			$lang = $destinationDocument->getLang();							
			
			$destinationDocument = $originalDocument->cloneToLang($lang);
						
			$destinationDocument->setLabel($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
								->setPublicationstatus("DRAFT");
								
			$content = $destinationDocument->getAllBlocks();
			
			foreach($content as $block)
			{
				$block["pageLang"] = $destinationDocument->getLang();
				$block["blockLang"] = $destinationDocument->getLang();
				
				if(isset($block["contentId"]))
				{
					unset($block["contentId"]);
				}
				if(isset($block["content"]))
				{
					unset($block["content"]);
				}
				if(isset($block["revision"]))
				{
					unset($block["revision"]);
				}
				$destinationDocument->setBlockParamsById($block["blockId"], $block);
			}
			
			$destinationDocument->save(false);
			
			$parent->addPage($destinationDocument)->save(false);
			
			return "redirectUrl=".$destinationDocument->getEditUrl($destinationDocument->getLang());
	}	
	
	private function translateWithContent($originalDocument, $destinationDocument)
	{
			$websiteId = $originalDocument->getWebsite()->getUniqueId();					
			$parent = $this->getParent($originalDocument, $destinationDocument);
			$lang = $destinationDocument->getLang();	
			$documentProvider = f_document_Provider::getInstance();						
			
			$destinationDocument = $originalDocument->cloneToLang($lang);
						
			$destinationDocument->setLabel($originalDocument->getLabel()." (".$destinationDocument->getLang().")")
								->setPublicationstatus("DRAFT");
								
			$content = $destinationDocument->getAllBlocks();
			
			foreach($content as $block)
			{
				$block["pageLang"] = $destinationDocument->getLang();
				$block["blockLang"] = $destinationDocument->getLang();
				if(isset($block["contentId"]))
				{
					$linkContent = $documentProvider->getByUniqueId($block["contentId"], $originalDocument->getLang());
					$newContent = $linkContent->cloneToLang($destinationDocument->getLang())->save(false);	
					$block["contentId"] = $newContent->getUniqueId();
				}
				if(isset($block["revision"]))
				{
					unset($block["revision"]);
				}

				$destinationDocument->setBlockParamsById($block["blockId"], $block);
			}
			
			$destinationDocument->save(false);
			
			$parent->addPage($destinationDocument)->save(false);
			
			return "redirectUrl=".$destinationDocument->getEditUrl($destinationDocument->getLang());		
	}
	
	private function getParent($originalDocument, $destinationDocument)
	{
        /* @var $originalDocument f_document_BaseDocument */
        $parent = $originalDocument->getDocumentParent();

        return $this->getDocumentProvider()->getByUniqueId($parent->getUniqueId(), $destinationDocument->getLang());
	}
}